package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.OrderDetail;
import com.zrrd.reggie.bean.Orders;
import com.zrrd.reggie.bean.OrdersDto;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.common.ThreadLocalUtil;
import com.zrrd.reggie.service.OrderDetailService;
import com.zrrd.reggie.service.OrdersService;
import com.zrrd.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String number,String beginTime,String endTime){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Orders> wrapper =new QueryWrapper<>();
        if(number!=null && !number.equals("")){
            wrapper.like("number",number);
        }
        if (beginTime!=null && !beginTime.equals("") && endTime!=null && !endTime.equals("")){
            wrapper.between("order_time",beginTime,endTime);
        }
        ordersService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    //修改订单状态
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改成功");
    }
    //用户下单逻辑
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }




    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){
        //创建分页对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        Long currentId = ThreadLocalUtil.getCurrentId();
        wrapper.eq("user_id",currentId);
        wrapper.orderByDesc("order_time");
        ordersService.page(pageInfo,wrapper);
        //以上是查询订单数据，后面是组装
        Page<OrdersDto> dtoPage =new Page<>();
        //复制 把PageInfo 复制给dto
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        //创建dtoList
        List<OrdersDto> dtoList = new ArrayList<>();
        List<Orders> records = pageInfo.getRecords();
        for (Orders orders : records) {
            OrdersDto dto = new OrdersDto();
            BeanUtils.copyProperties(orders,dto);
            //创建条件构造器
            QueryWrapper<OrderDetail> qw = new QueryWrapper<>();
            qw.eq("order_id",orders.getId());
            //根据订单id 查询订单明细
            List<OrderDetail> list = orderDetailService.list(qw);
            //给dto对象设置明细集合
            dto.setOrderDetails(list);
            //将dto加入
            dtoList.add(dto);
        }
        //设置dto的records
            dtoPage.setRecords(dtoList);
        //=
        return R.success(dtoPage);
    }
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){

        ordersService.again(orders);
        return R.success(null);
    }

}
